package com.tanhan.mindapp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhan.mindapp.dao.Result;
import com.tanhan.mindapp.dao.SysMenu;
import com.tanhan.mindapp.dao.SysMenuDTO.SysMenuQueryDTO;
import com.tanhan.mindapp.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/sysMenu")
@Tag(name = "系统菜单管理", description = "管理系统中菜单信息的增删改查操作")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/list")
    @Operation(summary = "查询所有菜单", description = "获取系统中所有菜单的列表信息")
    public Result<List<SysMenu>> list() {
        List<SysMenu> list = sysMenuService.listAllMenus();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询菜单", description = "根据菜单唯一标识符查询单个菜单的详细信息")
    public Result<SysMenu> getById(@PathVariable Long id) {
        SysMenu menu = sysMenuService.getMenuById(id);
        return Result.success(menu);
    }

    @PostMapping
    @Operation(summary = "新增菜单", description = "创建一个新的菜单信息")
    public Result<Void> save(@RequestBody SysMenu sysMenu) {
        boolean isSuccess = sysMenuService.addMenu(sysMenu);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error(9999, "新增菜单失败");
        }
    }

    @PutMapping
    @Operation(summary = "更新菜单", description = "根据菜单ID更新菜单信息")
    public Result<Void> update(@RequestBody SysMenu sysMenu) {
        boolean isSuccess = sysMenuService.updateMenu(sysMenu);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error(9999, "更新菜单失败");
        }
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询菜单", description = "根据条件分页查询菜单列表")
    public Result<IPage<SysMenu>> page(@RequestBody SysMenuQueryDTO queryDTO) {
        IPage<SysMenu> pageResult = sysMenuService.selectMenuPage(queryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除菜单", description = "根据菜单ID删除指定菜单")
    public Result<Void> delete(@PathVariable Long id) {
        boolean isSuccess = sysMenuService.deleteMenu(id);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error(9999, "删除菜单失败");
        }
    }

    @PostMapping("/batchDelete")
    @Operation(summary = "批量删除菜单", description = "根据菜单ID列表批量删除菜单")
    public Result<Void> batchDelete(@RequestBody List<Long> ids) {
        boolean isSuccess = sysMenuService.deleteBatchMenus(ids);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error(9999, "批量删除菜单失败");
        }
    }
}
